package com.kbt.util.tool;


import android.graphics.*;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * @author zhgk
 * @package: com.kbt.widget
 * @TODO: <请描述这个类是干什么的>
 * @data: 2015/8/10 17:14
 * @version: V1.0
 */
public class BtnSetting {
    /**
     * @desc: 设置按钮背景边框
     * @param btn 按钮
     * @param lineWidth 边框宽度
     * @param lineColor 边框颜色
     * @param backColor 按钮填充色
     * @param radius 圆角弧度
     */
    public static void setBtnBackground(Button btn,int lineWidth,int lineColor,int backColor,int radius){
        //创建背景形状
        GradientDrawable drawable = new GradientDrawable();
        //设置边框
        drawable.setShape(GradientDrawable.RECTANGLE);
        //设置边框长度和颜色
        drawable.setStroke(lineWidth, lineColor);
        //设置背景色
        drawable.setColor(backColor);
        //设置圆角
        drawable.setCornerRadius(radius);
        //设置按钮背景
        btn.setBackground(drawable);
    }
    public static void setLinBackground(LinearLayout btn,int lineWidth,int lineColor,int backColor,int radius){
        //创建背景形状
        GradientDrawable drawable = new GradientDrawable();
        //设置边框
        drawable.setShape(GradientDrawable.RECTANGLE);
        //设置边框长度和颜色
        drawable.setStroke(lineWidth, lineColor);
        //设置背景色
        drawable.setColor(backColor);
        //设置圆角
        drawable.setCornerRadius(radius);
        //设置按钮背景
        btn.setBackground(drawable);
    }
    public static void setTextViewBackground(View btn,int lineWidth,int lineColor,int backColor,int radius){
        //创建背景形状
        GradientDrawable drawable = new GradientDrawable();
        //设置边框
        drawable.setShape(GradientDrawable.RECTANGLE);
        //设置边框长度和颜色
        drawable.setStroke(lineWidth, lineColor);
        //设置背景色
        drawable.setColor(backColor);
        //设置圆角
        drawable.setCornerRadius(radius);
        //设置按钮背景
        btn.setBackground(drawable);
    }

    /**
     * 获取圆角位图的方法
     * @param bitmap 需要转化成圆角的位图
     * @param pixels 圆角的度数，数值越大，圆角越大
     * @return 处理后的圆角位图
     */
    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
